<?php /* УТФ-8:fix */

class SPImodel__feedback__items__manage extends SPImodel__feedback__items
{
    public function select($filter_search = '', $orderby = null )
    {
        switch( $orderby['dir'] )
        {
            default:
            case '+':   $orderby['dir'] = 'ASC';  break;
            case '-':   $orderby['dir'] = 'DESC'; break;
        }

        switch( $orderby['field'] )
        {
            default:
            case 'name':        $q_orderby = "name {$orderby['dir']}, id ASC"; break;
            case 'id':          $q_orderby = "id {$orderby['dir']}"; break;
            case 't_created':   $q_orderby = "t_created {$orderby['dir']}, id ASC"; break;
        }

        $store = SPIstore::I();

        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:feedback_items']
               .     " WHERE TRUE"
               . ( $filter_search
                 ? '     AND (  ' . $store->prepare_search_query( 'id', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'name', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'const_name', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'email', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'const_email', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'message', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'const_message', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'answer', $filter_search )
                 . '         ) '
                 : ''
                 )
               . " ORDER BY {$q_orderby} "
               ;

        return $store->query( $query );
    }

    public function get_by_id( $item_id )
    {
        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:feedback_items']
               .     " WHERE id=" . (int) $item_id
               ;

        return SPIstore::I()->query( $query )->fetch_and_close();
    }

    public function set_activity( $set_activity_value, $item_id )
    {
		return SPIstore::I()->modify(
            SPIcfg::$store['table:feedback_items'],
            array( 'is_public' => ($set_activity_value == 'on' ? 1 : 0 ) ),
            array( 'id' => $item_id )
        );
    }

    public function insert( $fields )
    {
        $fields['pid_profile']      = null; // SPI::I('Iam')->get_id();

        $result = SPIstore::I()->insert(
            SPIcfg::$store['table:feedback_items'],
            $fields,
            null,
            array( 'id' => SPIcfg::$store['sequence:global'] )
        );

        return $result === false ? false : $result['id'];
    }

    public function modify( $item_id, $fields )
    {
		return SPIstore::I()->modify(
            SPIcfg::$store['table:feedback_items'],
            $fields,
            array( 'id' => $item_id )
        );
    }

    public function delete( $item_id )
    {
		return SPIstore::I()->delete(
            SPIcfg::$store['table:feedback_items'],
            array( 'id' => $item_id )
        );
    }
}
